package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.Adress;
import com.elite.entity.Preorder;
import com.elite.entity.Seller;
import com.elite.entity.User;
import com.elite.service.AdressService;
import com.elite.service.PreorderService;
import com.elite.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by hbl on 2017/2/18.
 */
@RestController
@RequestMapping("/PreOrder")
public class PreorderController {
    @Autowired
    private PreorderService preorderService;
    @Autowired
    private AdressService adressService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private HttpSession session;

    private static final Logger LOGGER = Logger.getLogger(String.valueOf(PreorderController.class));


    @RequestMapping(value = "/getAddress",produces = "text/html;charset=UTF-8")
    public String getAddress(){
        User user = (User) session.getAttribute("loginUser");
        Integer userId = user.getUserId();
        List<Adress> adresses = adressService.selectAddressById(userId);
        return JSON.toJSON(adresses).toString();
    }

    /**
     *将数据加入表中
     * @param preorderCity
     * @param preorderName
     * @param preorderCount
     * @param preorderPhone
     * @param preorderAdess
     * @param preorderCuttime
     * @param preoderPrice
     * @param preoderUnit
     * @param preoderTotalprice
     */
    @RequestMapping(value = "/push",method = RequestMethod.POST)
    public void push(String preorderCity,
                     String preorderName,Integer preorderCount,String preorderPhone,String preorderAdess,
                     String preorderCuttime,Float preoderPrice,String preoderUnit,Float preoderTotalprice){
        User user = (User) session.getAttribute("loginUser");
        Integer userId1 = user.getUserId();
//        Seller seller = sellerService.findByUserId(userId1);
//        Integer sellerId = seller.getSellerId();
        Preorder preorder = new Preorder();
        preorder.setUserId(userId1);
        preorder.setSellerId(null);
        preorder.setPreorderStatus(0);
        preorder.setPreorderCity(preorderCity);
        preorder.setPreorderName(preorderName);
        preorder.setPreorderCount(preorderCount);
        preorder.setPreorderPhone(preorderPhone);
        preorder.setPreorderAdess(preorderAdess);
        preorder.setPreorderCuttime(preorderCuttime);
        preorder.setPreoderPrice(preoderPrice);
        preorder.setPreoderUnit(preoderUnit);
        preorder.setPreoderTotalprice(preoderTotalprice);
        preorderService.insertAll(preorder);
        LOGGER.info(String.valueOf(preorder));
    }

    /**
     * 将数据库中的信息传到前端0
     * @return
     */
    @RequestMapping(value = "/getAll",produces = "text/html;charset=UTF-8")
    public String getAll(){
      List<Preorder> preorderList = preorderService.getAll();
        List<Preorder> preorders = new ArrayList<Preorder>();

        Iterator pre = preorderList.iterator();
        while (pre.hasNext()){
            Preorder preorder = (Preorder) pre.next();
            Integer statu = preorder.getPreorderStatus();
            if(statu==0){
                preorders.add(preorder);
            }
        }
        return JSON.toJSON(preorders).toString();
    }

    /**
     * 将数据库中的信息传到前端1
     * @return
     */
    @RequestMapping(value = "/getAllStatus",produces = "text/html;charset=UTF-8")
    public String getAllStatus(){
        List<Preorder> preorderList = preorderService.getAll();
        List<Preorder> preorders = new ArrayList<Preorder>();

        Iterator pre = preorderList.iterator();
        while (pre.hasNext()){
            Preorder preorder = (Preorder) pre.next();
            Integer statu = preorder.getPreorderStatus();
            if(statu==1){
                preorders.add(preorder);
            }
        }
        return JSON.toJSON(preorders).toString();
    }

    /**
     * 通过id来删除
     * @param preorderId
     */
    @RequestMapping(value = "/change",method = RequestMethod.POST)
    public void change(Integer preorderId){
        User user = (User) session.getAttribute("loginUser");
        Integer userId1 = user.getUserId();
        Seller seller = sellerService.findByUserId(userId1);
        Integer sellerId = seller.getSellerId();
        Preorder preorder = new Preorder();
        preorder.setPreorderStatus(1);
        preorder.setPreorderId(preorderId);
        preorder.setSellerId(sellerId);
        preorderService.changeById(preorder);
    }

    /**
     * 将数据库中的信息传到前端--卖家--除开状态已经为5(完成交易)的  除开0 大于本身 小于5
     * @return
     */
    @RequestMapping(value = "/getSeller",produces = "text/html;charset=UTF-8")
    public String getSeller(){

        User user = (User) session.getAttribute("loginUser");
        Integer userId1 = user.getUserId();
        Seller seller = sellerService.findByUserId(userId1);
        Integer sellerId = seller.getSellerId();

        List<Preorder> preorderList = preorderService.getAll();
        List<Preorder> preorders = new ArrayList<Preorder>();

        Iterator pre = preorderList.iterator();
        while (pre.hasNext()){
            Preorder preorder = (Preorder) pre.next();
            Integer statu = preorder.getPreorderStatus();
            if(statu>0 && statu<5){
                if (preorder.getSellerId()==sellerId){
                    preorders.add(preorder);
                }
            }
        }
        return JSON.toJSON(preorders).toString();
    }

    /**
     * 通过传入的参数去改变状态码，还有一个id
     * @param status
     */
    @RequestMapping(value = "/changeStatus",method = RequestMethod.POST)
    public void changeStatus(Integer status,Integer preorderId){
        Preorder preorder = new Preorder();
        preorder.setPreorderId(preorderId);
        preorder.setPreorderStatus(status);
        preorderService.changeStatusById(preorder);
    }


    /**
     * 将数据库中的信息传到前端--用户--除开状态已经为5(完成交易)的  除开0
     * @return
     */
    @RequestMapping(value = "/getUser",produces = "text/html;charset=UTF-8")
    public String getUser(){

        User user = (User) session.getAttribute("loginUser");
        Integer userId1 = user.getUserId();

        List<Preorder> preorderList = preorderService.getAll();
        List<Preorder> preorders = new ArrayList<Preorder>();

        Iterator pre = preorderList.iterator();
        while (pre.hasNext()){
            Preorder preorder = (Preorder) pre.next();
            Integer statu = preorder.getPreorderStatus();
            if(statu!=0 && statu!=5){
                if (preorder.getUserId()==userId1){
                    preorders.add(preorder);
                }
            }
        }
        return JSON.toJSON(preorders).toString();
    }

    /**
     * 通过得到的id号给出一个对象
     * @param preorderId
     * @return
     */
    @RequestMapping(value = "/getById",produces = "text/html;charset=UTF-8")
    public String getById(Integer preorderId){
        Preorder preorder = preorderService.selectById(preorderId);
        return JSON.toJSON(preorder).toString();
    }


}
